import { getOptionsByUrl } from "@/api/system/common";

export const useUrlDictStore = defineStore("url-dict", () => {
  const dictData = reactive<Map<string, Custom.TreeData>>(new Map());

  async function fetchUrl(url: string) {
    if (dictData.has(url)) return dictData.get(url);
    // await sleep(9000);
    const [err, res] = await getOptionsByUrl(url, { loading: false });
    // console.log(`fetch ${url}: `, err, res);
    if (!err) {
      dictData.set(url, res);
      return res;
    } else console.error(`request ${url} error: `, err);
  }

  return { dictData, fetchUrl };
});
